import {
  prop,
  getModelForClass,
  modelOptions,
  Severity,
} from "@typegoose/typegoose";

@modelOptions({
  schemaOptions: {
    toJSON: {
      versionKey: false,
      virtuals: true,
      transform: (doc, ret) => {
        ret.id = ret._id;
        delete ret._id;
        return ret;
      },
    },
    toObject: {
      versionKey: false,
      virtuals: true,
    },
  },
})
export class Article {
  @prop({ required: true, unique: true })
  title: string;

  @prop({ required: true })
  content: string;

  // @prop({ required: true, allowMixed: Severity.ALLOW })
  @prop({ required: [true, "tags is required"], type: () => [String] })
  tags: string[];

  @prop({ required: true })
  author: string;

  @prop()
  cover: string;

  @prop({ required: true, default: Date.now })
  createdAt: Date;

  @prop({ required: true, default: Date.now })
  updatedAt: Date;
}

export const ArticleModel = getModelForClass(Article);
